package com.mo.common;

import com.mo.enums.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
@AllArgsConstructor
public class Result {
    String msg;
    Boolean success;
    Integer code;
    Object data;

    /**
     * 默认成功
     *
     * @return Result
     */
    public static Result success() {
        return Result.builder()
                .code(ResultCode.SUCCESS.getCode())
                .msg(ResultCode.SUCCESS.getMessage())
                .success(true)
                .build();
    }

    /**
     * 自定义的 ResultCode
     *
     * @param resultCode 返回的枚举类
     * @return ResultCode
     */
    public static Result success(ResultCode resultCode) {
        return Result.builder()
                .code(resultCode.getCode())
                .msg(resultCode.getMessage())
                .success(true)
                .build();
    }

    /**
     * 默认成功 + 返回的数据
     *
     * @param data 返回的数据
     * @return Object
     */
    public static Result success(Object data) {
        return Result.builder()
                .code(ResultCode.SUCCESS.getCode())
                .msg(ResultCode.SUCCESS.getMessage())
                .success(true)
                .data(data)
                .build();
    }

    /**
     * 默认错误
     *
     * @return ResultCode
     */
    public static Result error() {
        return Result.builder()
                .success(false)
                .code(ResultCode.ERROR.getCode())
                .msg(ResultCode.ERROR.getMessage())
                .build();
    }

    /**
     * 默认错误
     *
     * @return ResultCode
     */
    public static Result error(String msg, Integer code) {
        return Result.builder()
                .success(false)
                .code(ResultCode.ERROR.getCode())
                .msg(msg)
                .build();
    }

    /**
     * 自定义的 ResultCode
     *
     * @param resultCode 定义的 ResultCode
     * @return ResultCode
     */
    public static Result error(ResultCode resultCode) {
        return Result.builder()
                .success(false)
                .code(resultCode.getCode())
                .msg(resultCode.getMessage())
                .build();
    }
}
